const Router = require('koa-router')
const callCloudDB = require('../../../core/callCloudDB.js')
const cloudStorage = require('../../../core/callCloudStorage.js')
// get post

const router = new Router({
    prefix: '/v1/blog'
})

router.get('/list', async (ctx, next) => { //列表
    const params = ctx.request.query
    const query = `
        db.collection('blog').skip(${params.start}).limit(${params.count}).orderBy('createTime', 'desc').get()
    `
    const res = await callCloudDB(ctx, 'databasequery', query)
    let data = res.data.map(item => JSON.parse(item))

    ctx.body = {
        error_code: 0,
        msg: 'ok',
        data:{
            list:data,
            total:100
        }
    }
})

router.post('/del', async (ctx, next) => {
    const params = ctx.request.body
    // 删除blog
    const queryBlog = `db.collection('blog').doc('${params._id}').remove()`
    const delBlogRes = await callCloudDB(ctx, 'databasedelete', queryBlog)
    
    // 删除blog-comment
    const queryComment = `db.collection('blog-comment').where({
        blogId: '${params._id}'
    }).remove()`
    const delCommentRes = await callCloudDB(ctx, 'databasedelete', queryComment)
    
    // 删除图片
    const delStorageRes = await cloudStorage.delete(ctx, params.img)
    ctx.body = {
        code: 200,
        data: delStorageRes
    }
})

module.exports = router